package org.example.salesystem.controller;

import org.example.salesystem.entity.Complaint;
import org.example.salesystem.service.ComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/complaint")
@CrossOrigin(origins = "*")
public class ComplaintController {
    @Autowired
    private ComplaintService complaintService;

    @PostMapping("/approve")
    public ResponseEntity<?> approveComplaint(@RequestBody Map<String, Object> params) {
        try {
            Long id = Long.valueOf(params.get("id").toString());
            String productId = params.get("product_id").toString();
            String reporterPhone = params.get("reporter_phone").toString();
            
            complaintService.approveComplaint(id, productId, reporterPhone);
            
            Map<String, Object> response = new HashMap<>();
            response.put("status", 200);
            response.put("message", "投诉已通过");
            response.put("data", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(createErrorResponse(400, "参数错误"));
        }
    }

    @PostMapping("/reject")
    public ResponseEntity<?> rejectComplaint(@RequestBody Map<String, Object> params) {
        try {
            Long id = Long.valueOf(params.get("id").toString());
            String productId = params.get("product_id").toString();
            String reporterPhone = params.get("reporter_phone").toString();
            String feedback = params.get("feedback").toString();
            
            complaintService.rejectComplaint(id, productId, reporterPhone, feedback);
            
            Map<String, Object> response = new HashMap<>();
            response.put("status", 200);
            response.put("message", "投诉已拒绝");
            response.put("data", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(createErrorResponse(400, "参数错误"));
        }
    }

    @PostMapping("/seller/list")
    public ResponseEntity<?> getSellerComplaints() {
        try {
            List<Complaint> complaints = complaintService.getSellerComplaints();
            
            Map<String, Object> response = new HashMap<>();
            response.put("status", 200);
            response.put("message", "获取成功");
            response.put("data", complaints);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(createErrorResponse(500, "服务器内部错误"));
        }
    }

    @PostMapping("/user/list")
    public ResponseEntity<?> getUserComplaints(@RequestBody Map<String, Object> params) {
        try {
            String reporterPhone = params.get("reporter_phone").toString();
            List<Complaint> complaints = complaintService.getUserComplaints(reporterPhone);
            
            Map<String, Object> response = new HashMap<>();
            response.put("status", 200);
            response.put("message", "获取成功");
            response.put("data", complaints);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(createErrorResponse(400, "参数错误"));
        }
    }

    private Map<String, Object> createErrorResponse(int status, String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("status", status);
        response.put("message", message);
        response.put("data", null);
        return response;
    }
} 